{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3a49594f",
   "metadata": {},
   "source": [
    "# NumPy 数组形状\n",
    "## 数组的形状\n",
    "数组的形状是每个维中元素的数量。\n",
    "\n",
    "## 获取数组的形状\n",
    "NumPy 数组有一个名为 shape 的属性，该属性返回一个元组，每个索引具有相应元素的数量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "de95a8f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [5, 6, 7, 8]])\n",
    "\n",
    "print(arr.shape)\n",
    "# 返回 (3, 4)，这意味着该数组有 2 个维，每个维有 4 个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7b6595f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[[[1 2 3 4]]]]]\n",
      "shape of array : (1, 1, 1, 1, 4)\n"
     ]
    }
   ],
   "source": [
    "# 利用 ndmin 使用值 1,2,3,4 的向量创建有 5 个维度的数组，并验证最后一个维度的值为 4\n",
    "arr = np.array([1, 2, 3, 4], ndmin=5)\n",
    "\n",
    "print(arr)\n",
    "print('shape of array :', arr.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d3d4bd9",
   "metadata": {},
   "source": [
    "# NumPy 数组重塑\n",
    "## 数组重塑\n",
    "重塑意味着更改数组的形状。\n",
    "\n",
    "数组的形状是每个维中元素的数量。\n",
    "\n",
    "通过重塑，我们可以添加或删除维度或更改每个维度中的元素数量。\n",
    "\n",
    "## 从 1-D 重塑为 2-D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "28f07db2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12,)\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "(4, 3)\n"
     ]
    }
   ],
   "source": [
    "# 将以下具有 12 个元素的 1-D 数组转换为 2-D 数组\n",
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n",
    "\n",
    "newarr = arr.reshape(4, 3)\n",
    "\n",
    "print(arr.shape)\n",
    "print(newarr)\n",
    "print(newarr.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dced893",
   "metadata": {},
   "source": [
    "## 从 1-D 重塑为 3-D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "beb74759",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1  2]\n",
      "  [ 3  4]\n",
      "  [ 5  6]]\n",
      "\n",
      " [[ 7  8]\n",
      "  [ 9 10]\n",
      "  [11 12]]]\n",
      "(2, 3, 2)\n"
     ]
    }
   ],
   "source": [
    "# 将以下具有 12 个元素的 1-D 数组转换为 3-D 数组\n",
    "newarr = arr.reshape(2, 3, 2)\n",
    "\n",
    "print(newarr)\n",
    "print(newarr.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed6646ef",
   "metadata": {},
   "source": [
    "## 我们可以重塑成任何形状吗？\n",
    "是的，只要重塑所需的元素在两种形状中均相等。\n",
    "\n",
    "我们可以将 8 元素 1D 数组重塑为 2 行 2D 数组中的 4 个元素，但是我们不能将其重塑为 3 元素 3 行 2D 数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8a884973",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot reshape array of size 8 into shape (3,3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-428f107e9370>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0marr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m8\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mnewarr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnewarr\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 8 into shape (3,3)"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "newarr = arr.reshape(3, 3)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6d677c4",
   "metadata": {},
   "source": [
    "## 返回副本还是视图？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f1b17707",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "print(arr.reshape(2, 4).base)\n",
    "# 上面的例子返回原始数组，因此它是一个视图"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24003fcd",
   "metadata": {},
   "source": [
    "## 未知的维\n",
    "您可以使用一个“未知”维度。\n",
    "\n",
    "这意味着您不必在 reshape 方法中为维度之一指定确切的数字。\n",
    "\n",
    "传递 -1 作为值，NumPy 将为您计算该数字。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d0ebb583",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
    "\n",
    "newarr = arr.reshape(2, 2, -1)\n",
    "\n",
    "print(newarr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b34abd26",
   "metadata": {},
   "source": [
    "## 展平数组\n",
    "展平数组（Flattening the arrays）是指将多维数组转换为 1D 数组。\n",
    "\n",
    "我们可以使用 reshape(-1) 来做到这一点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "41c4a325",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "newarr = arr.reshape(-1)\n",
    "\n",
    "print(newarr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
